# -*- coding: utf8 -*-

import logging
from fractions import Fraction as frac

logger = logging.getLogger(__name__)


def train(clf, read_data_set):
    px_vec, lb_vec = read_data_set()

    logger.debug('Constructing classifier...')
    clf.fit(px_vec, lb_vec)
    logger.debug('Done constructing classifier.')

    logger.debug('Predicting...')
    pr_vec = clf.predict(px_vec)
    logger.debug('Done predicting.')

    logger.debug('Comparing predictions...')
    result = list(filter(None, (label == prediction for label, prediction in zip(lb_vec, pr_vec))))
    logger.info('Matched Result: %d, %.2f.' % (len(result), len(result) / len(lb_vec)))
    logger.debug('Done comparing predictions.')

    return frac(len(result), len(lb_vec))


def test(clf, read_data_set):
    ref_px_vec, ref_lb_vec = read_data_set()

    logger.debug('Predicting reference vectors...')
    ref_pr_vec = clf.predict(ref_px_vec)
    logger.debug('Done predicting reference vectors.')

    logger.debug('Comparing reference predictions...')
    ref_result = list(filter(None, (label == prediction for label, prediction in zip(ref_lb_vec, ref_pr_vec))))
    logger.info('Matched Result: %d, %.2f' % (len(ref_result), len(ref_result) / len(ref_lb_vec)))
    logger.debug('Done comparing reference predictions.')

    return frac(len(ref_result), len(ref_lb_vec))
